// pages/login/login.js
let timer = null;
const IsPhone = /^1[345789]\d{9}$/;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wxsrc: '../image/wx.png',
    codeMsg: "获取验证码",
    time: 10,
    phone: "",
    canClick: true,
    checkImg: '../image/checked.png',
    unCheckImg: '../image/unchecked.png',
    agreeStatus: false,
    code: "",
    invite_code: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    console.log(options)
    if (options.invite_code) {
      that.setData({
        invite_code: options.invite_code
      })
    }
  },
  
  // 登录
  handleSubmit() {
    if (!IsPhone.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!this.data.code) {
      wx.showToast({
        title: '请输入短信验证码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!this.data.agreeStatus) {
      wx.showToast({
        title: '请勾选用户服务协议',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    wx.request({
      url: 'https://api.bshlsqs.com/passport/register',
      data: {
        mobile: this.data.phone,
        code: this.data.code,
        invite_code: this.data.code,
      },
      success(res) {
        if(res.data.code == 0) {
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 2000
          })

          setTimeout(() => {
            wx.redirectTo({
              url: '../index/index',
            })
          }, 2 * 1000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      
    })
  },
  // 手机号码
  phoneInput(e) {
    var val = e.detail.value
    this.setData({
      phone: val
    })
  },
  // 邀请码
  inviteCodeInput(e) {
    var val = e.detail.value
    this.setData({
      invite_code: val
    })
  },
  // 验证码
  codeInput(e) {
    var val = e.detail.value
    this.setData({
      code: val
    })
  },
  // 同意
  agreeFun() {
    this.setData({
      agreeStatus: !this.data.agreeStatus
    })
  },
  // 获取验证码
  getCode() {
    if (!this.data.canClick) {
      return;
    }
    if (!IsPhone.test(this.data.phone)) {

      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.data.canClick = false;
    this.countDown();
    this.getSmsCode();
  },

  getSmsCode() {
    wx.request({
      url: 'https://api.bshlsqs.com/passport/send_phone_code',
      data: {
        mobile: this.data.phone,
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 0) {
          wx.showToast({
            title: '验证码已发送',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  countDown() {
    timer = setTimeout(() => {
      this.data.time -= 1;
      this.setData({
        codeMsg: `剩余${this.data.time}秒`,
      })

      if (this.data.time > 0) {
        this.countDown();
      } else {

        this.setData({
          codeMsg: `重新发送`,
        })
        clearTimeout(timer);
        timer = null;
        this.data.time = 10;
        this.data.canClick = true;
      }
    }, 1 * 1000);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})